var fDs;
var fGrid;
var fForm;
Ext.onReady(function()
{
	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});	
	var recordType = Ext.data.Record.create([								
                			   {name: 'id', type: "string"},
							   {name: 'no', type: "string"},
                	           {name: 'ocName', type: "string"},
                	           {name: 'fiberType', type: "string"},
							   {name: 'testLength', type: "string"},
							   {name: 'usedInfo', type: "string"}
                	     ]);

	var cm  = new Ext.grid.ColumnModel(
			[
				sm,	            
	            {header: "中继段名称", width: 100, sortable: true, dataIndex: 'ocName',align:'left'},
	            {header: "光纤类型", width: 100, sortable: true, dataIndex: 'fiberType',align:'left'},
				{header: "纤芯测试长度", width: 100, sortable: true, dataIndex: 'testLength',align:'left'},
				{header: "纤芯使用情况", width: 100, sortable: true, dataIndex: 'usedInfo',align:'left'},				
				{header: "删除", width: 50, dataIndex: 'id',renderer: showDelete}					
			]
		);
		
	function showDelete(value, cellmeta, record, rowIndex, columnIndex, store)
	{
		var ocid = record.get("ocid");
		return "<input type='button' value='删除' class='x-btn button' onclick='doFInfo(\"" + value  +   "\",\""+ocid+"\")'>";  
	}
	

	fDs = new Ext.data.Store({
        proxy: new Ext.data.DWRProxy(JFiberWeb.getFiberCoreList, true),	
        reader: new Ext.data.ListRangeReader({
            totalProperty: 'totalSize',
            id: 'id'
        }, recordType),
        sortInfo:{field:'id', direction:'ASC'}// 排序信息
    });
	
	fGrid = new Ext.grid.GridPanel({	    		
			region : 'center',		
	        ds: fDs,
	        cm: cm,
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {  
                            fForm.getForm().loadRecord(rec);
                        }
                    }
                }),
			height: 400,
			minSize: 200,
	        frame:true,
	        collapsible:false, 
	        enableDragDrop:false,   
			bbar: new Ext.PagingToolbar({
			        pageSize: 17,
			        store: fDs,
			        displayInfo: true,
			        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			        emptyMsg: "没有记录"
			    })				
				
	    });
	//fDs.load({params:{id:"<%=strId%>",start:0,limit:17}});	
		
	fForm = new Ext.FormPanel({
		region     : 'east', 
        labelWidth: 95,
        frame:true,
        title: '总体信息', 
        width: 300,
		height:360,
        defaults: {width: 180},
        defaultType: 'textfield',
        items: [
				{
					xtype : 'hidden',
					name: 'id'
				},
				{
					xtype : 'hidden',
					name: 'no'
				}
				,{
					fieldLabel: '中继段名称',
					name: 'ocName', 
					readOnly:true	
				},{
					fieldLabel: '光纤类型',
					name: 'fiberType',
					allowBlank:false,
					blankText :'请输入光纤类型'	
				},{
					xtype:'numberfield',
					allowDecimals: true, 
					fieldLabel: '纤芯测试长度',
					name: 'testLength',
					allowBlank:false,
					blankText :'请输入纤芯测试长度'	
				},
				{					 
					fieldLabel: '纤芯使用情况',
					name: 'usedInfo'
				}
			],
        buttons: [
				{
					text: '后退',
					handler:function()
					{
						history.go(-1);
					}
				},{
					text: '保存',
					handler:saveSectionInfo
				},{
					text: '新建/取消',
					handler:function()
					{
						fForm.getForm().reset(); 
						resetForm(fForm);
					}
				}]
		});	
	new Ext.Panel({
		width    : mainTabPanelDIV.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'usedTab',
		items    : [
			fGrid,
			fForm
		]
	});	
	
	//loadDate(fDs,fForm);
	
	function saveSectionInfo()
	{
		if (!fForm.getForm().isValid())
			return;	
		var form = fForm.getForm().getValues(false);
		FibercoreBO = {};
		FibercoreBO.id = form.id;
		FibercoreBO.no = ocid;
		FibercoreBO.ocName = form.ocName;
		FibercoreBO.fiberType = form.fiberType;
		FibercoreBO.testLength = form.testLength;
		FibercoreBO.usedInfo = form.usedInfo;
		
		JFiberWeb.createFiberCore(FibercoreBO,function(data)
		{ 
			fForm.getForm().reset(); 
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				//fDs.load({params:{id:'<%=strId%>',start:0,limit:17}});
				fForm.getForm().reset();
				loadDate(fDs,fForm);
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}	
		})
	}
		
})

function doFInfo(id,ocid)
{	
	
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {	 
			 fForm.getForm().reset(); 
			 JFiberWeb.deleteFiberCore(id, function(data) {
			 if(data) {							    	     		
				 //fDs.load({params:{id:'<%=strId%>',start:0,limit:17}});
				 loadDate(fDs,fForm);
			 }else 
			 {
				Ext.Msg.alert("删除失败","数据删除失败!");
			 }
			 });
			
		 }
	});
	
}
